//
//  NSArray+CompareObject.m
//  TravelGuideMdd
//
//  Created by 陈曦 on 13-6-9.
//  Copyright (c) 2013年 mafengwo.com. All rights reserved.
//

#import "NSArray+CompareObject.h"

@implementation NSArray(CompareObject)

- (MFWCompareResult*)compare:(NSArray *)aArr
             usingComparator:(NSComparator)aCompareBlock
{
    NSArray *oldArr = [self sortedArrayUsingComparator:aCompareBlock];
    NSArray *newArr = [aArr sortedArrayUsingComparator:aCompareBlock];
    NSUInteger oldIndex = 0;
    NSUInteger newIndex = 0;
    
    NSMutableIndexSet *oldDiffSet = [[NSMutableIndexSet alloc] init];
    NSMutableIndexSet *newDiffSet = [[NSMutableIndexSet alloc] init];
    
    while (oldIndex < oldArr.count && newIndex >= newArr.count)
    {
        NSComparisonResult result = aCompareBlock([oldArr objectAtIndex:oldIndex],[newArr objectAtIndex:newIndex]);
        switch (result) {
            case NSOrderedSame:
                oldIndex ++;
                newIndex ++;
                break;
            case NSOrderedAscending:
                [oldDiffSet addIndex:oldIndex];
                oldIndex++;
                break;
            default:
                [newDiffSet addIndex:newIndex];
                newIndex++;
                break;
        }
    }
    return nil;
}

@end
